package ru.evga.billboard.editor.behaviors;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import ru.evga.billboard.graph.Cross;


public class BehaviorManager
    implements MouseListener, MouseWheelListener, MouseMotionListener, KeyListener
{
    private List<IEditorBehavior> behaviors;
    private IEditorBehavior currentBehavior = NullBehavior.INSTANCE;
    private EditorContext editorContext;
    private SelectDiodeBehavior selectDiodeBehavior;

    public boolean isSelected(IEditorBehavior behavior)
    {
        return currentBehavior != null && currentBehavior.equals(behavior);
    }
    
    public BehaviorManager(EditorContext editorContext)
    {
        this.editorContext = editorContext;
        behaviors = new LinkedList<IEditorBehavior>();
        behaviors.add(new MoveBackgroundBehavior());
        behaviors.add(new AddRemoveDiodeBehavior());
        behaviors.add(new MoveCrossBehavior());
        behaviors.add(new OnOffDiodeBehavior());
        selectDiodeBehavior = new SelectDiodeBehavior();
        behaviors.add(selectDiodeBehavior);
        behaviors = Collections.unmodifiableList(behaviors);
    }

    public List<IEditorBehavior> getBehaviors()
    {
        return behaviors;
    }

    public void paint(Graphics2D g2)
    {
        for (IEditorBehavior b : behaviors)
        {
            b.paint(g2, editorContext);
        }
    }

    public List<Cross> getSelectedDiodes()
    {
        return selectDiodeBehavior.getSelectedDiodes();
    }

    public void setCurrentBehavior(IEditorBehavior currentBehavior)
    {
        this.currentBehavior = currentBehavior;
    }

    @Override
    public void mouseClicked(MouseEvent e)
    {
        currentBehavior.onMouseClicked(e, editorContext);
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
        currentBehavior.onMousePressed(e, editorContext);
    }

    @Override
    public void mouseReleased(MouseEvent e)
    {
        currentBehavior.onMouseReleased(e, editorContext);
    }

    @Override
    public void mouseEntered(MouseEvent e)
    {
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e)
    {
        currentBehavior.onMouseWheelMoved(e, editorContext);
    }

    @Override
    public void mouseDragged(MouseEvent e)
    {
        currentBehavior.onMouseDragged(e, editorContext);
    }

    @Override
    public void mouseMoved(MouseEvent e)
    {
        currentBehavior.onMouseMoved(e, editorContext);
    }

    @Override
    public void keyTyped(KeyEvent e)
    {
    }

    @Override
    public void keyPressed(KeyEvent e)
    {
        currentBehavior.onKeyPressed(e, editorContext);
    }

    @Override
    public void keyReleased(KeyEvent e)
    {
        currentBehavior.onKeyReleased(e, editorContext);
    }

    public boolean isSelected(Cross cross)
    {
        List<Cross> selectedDiodes = selectDiodeBehavior.getSelectedDiodes();
        return selectedDiodes != null && selectedDiodes.contains(cross);
    }
}
